package lambdasinaction.chap6;

import static java.util.stream.Collectors.reducing;
import static lambdasinaction.chap6.Dish.menu;

/**
 * Created by ThinkPad on 2017/8/19.
 */
public class Reducing {

    public static void main(String... args) {
        System.out.println("Total calories in menu: " + calculateTotalCalories());
        System.out.println("Total calories in menu: " + calculateTatolCaloriesWithMethodReference());
        System.out.println("Total calories in menu: " + calculateTotalCaloriesWithoutCollectors());
        System.out.println("Total calories in menu: " + calculateTotalCaloriesUsingSum());
    }

    private static int calculateTotalCalories() {
        return menu.stream().collect(reducing(0,Dish::getCalories,(Integer i,Integer j) -> i + j));
    }

    private static int calculateTatolCaloriesWithMethodReference() {
        return menu.stream().collect(reducing(0,Dish::getCalories,Integer::sum));
    }

    private static int calculateTotalCaloriesWithoutCollectors() {
        return menu.stream().map(Dish::getCalories)
                .reduce(Integer::sum).get();
    }

    private static int calculateTotalCaloriesUsingSum() {
        return menu.stream().mapToInt(Dish::getCalories).sum();
    }
}
